package cn.fanqinghong.pet.service.impl;

import cn.fanqinghong.basic.util.BaiduAuditUtils;
import cn.fanqinghong.org.domain.Employee;
import cn.fanqinghong.org.mapper.EmployeeMapper;
import cn.fanqinghong.pet.domain.Pet;
import cn.fanqinghong.pet.domain.PetDetail;
import cn.fanqinghong.pet.domain.PetOnlineAuditLog;
import cn.fanqinghong.pet.mapper.PetDetailMapper;
import cn.fanqinghong.pet.mapper.PetMapper;
import cn.fanqinghong.pet.mapper.PetOnlineAuditLogMapper;
import cn.fanqinghong.pet.service.IPetService;
import cn.fanqinghong.basic.service.impl.BaseServiceImpl;
import cn.fanqinghong.pet.vo.Onsale;
import cn.fanqinghong.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fanqinghong
 * @since 2022-06-25
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    /**
     * 修改
     * @param pet
     * @param logininfo
     */
    @Override
    public void update(Pet pet,Logininfo logininfo) {

    }

    /**
     * 新增
     * @param pet
     * @param logininfo
     */
    @Override
    public void save(Pet pet,Logininfo logininfo) {
        //通过登陆人的id到员工信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //设置当前店铺id
        pet.setShopId(employee.getShop_id());
        //保存宠物信息
        petMapper.save(pet);
        //获取到前端传递的宠物详情
        PetDetail petDetail = pet.getDetail();
        if (petDetail != null){
            //设置宠物id
            petDetail.setPetId(pet.getId());
            //保存宠物的详情
            petDetailMapper.save(petDetail);
        }


    }

    /**
     * 宠物上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        //审核宠物
        for (Long id : ids) {//遍历循环传入的id数组
            //通过id查找宠物
            Pet pet = petMapper.loadById(id);
            //创建一个可变字符串用来给前端返回数据
            StringBuilder sb = new StringBuilder();
            //审核宠物的名字是否违规
           /* if (!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append(pet.getName()+"名字不合法！！");
            }

            if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+pet.getResources().split(",")[0])){
                sb.append(pet.getResources()+"图片违规哦！！");
            }*/
            //1.1 审核不通过？ 记录审核日志，纪律审核时间，跳过，下一个继续
            if (sb.toString().length() > 1){//表示审核不通过，记录日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setNote(sb.toString());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                continue;//结束审核失败，跳到下一次循环
            }
            //1.2 修改上架状态，记录上架时间
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("onSaleTime",new Date());
            petMapper.onSale(map);

        }

    }

    /**
     * 宠物下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        //创建一个集合
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        //设置下架时间
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);


    }
}
